package org.indp.mongowl.ui;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.event.FieldEvents;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;
import org.indp.mongowl.services.ServerConnection;

/**
 *
 */
public class DatabaseBrowserComponent extends HorizontalSplitPanel {

    private final ServerConnection serverConnection;
    private final String databaseName;

    public DatabaseBrowserComponent(ServerConnection serverConnection, String databaseName) {
        this.serverConnection = serverConnection;
        this.databaseName = databaseName;

        setSizeFull();
        setSplitPosition(20, Unit.PERCENTAGE);
        setStyleName(Reindeer.SPLITPANEL_SMALL);
        setFirstComponent(createCollectionsTable());
        replaceDetails(new Label());
    }

    private void replaceDetails(Component component) {
        setSecondComponent(component);
    }

    private Component createCollectionsTable() {
        final IndexedContainer container = new IndexedContainer();
        container.addContainerProperty("name", String.class, "");

        final Table table = new Table();
        table.setSizeFull();
        table.setContainerDataSource(container);
        table.setItemCaptionPropertyId("name");
        table.setSelectable(true);
        table.setColumnHeaderMode(Table.ColumnHeaderMode.HIDDEN);
        table.setNullSelectionAllowed(false);
        table.setImmediate(true);
        table.addValueChangeListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(Property.ValueChangeEvent event) {
                Object value = event.getProperty().getValue();
                if (value == null) {
                    replaceDetails(new Label());
                }else {
                    showCollection(value.toString());
                }
            }
        });

        DB db = serverConnection.getDatabase(databaseName);
        for (String name : db.getCollectionNames()) {
            Item item = container.addItem(name);
            item.getItemProperty("name").setValue(name);
        }

        TextField searchField = new TextField();
        searchField.setWidth("100%");
        searchField.setInputPrompt("filter");
        searchField.setStyleName(Reindeer.TEXTFIELD_SMALL);
        searchField.addTextChangeListener(new FieldEvents.TextChangeListener() {
            @Override
            public void textChange(FieldEvents.TextChangeEvent event) {
                container.removeAllContainerFilters();
                container.addContainerFilter("name", event.getText(), true, false);
            }
        });

        VerticalLayout layout = new VerticalLayout(table, searchField);
        layout.setExpandRatio(table, 1);
        layout.setSizeFull();

        return layout;
    }

    private void showCollection(String name) {
        DB db = serverConnection.getDatabase(databaseName);
        DBCollection collection = db.getCollection(name);
        replaceDetails(new CollectionBrowserComponent(serverConnection, collection));
    }
}
